package com.yunquan.ohana.ui.calendar.sync.syncNew

import android.app.Application
import android.os.Handler
import android.os.Looper
import com.google.gson.Gson
import com.yunquan.ohana.R
import com.yunquan.ohana.base.bus.RxBus
import com.yunquan.ohana.base.bus.event.SingleLiveEvent
import com.yunquan.ohana.base.ui.BaseViewModel
import com.yunquan.ohana.db.DBFrames
import com.yunquan.ohana.dialog.LoadingDialog
import com.yunquan.ohana.dialog.TipDialog
import com.yunquan.ohana.entity.EventEntity
import com.yunquan.ohana.entity.EventType
import com.yunquan.ohana.entity.FramesDataManager
import com.yunquan.ohana.entity.FramesEntity
import com.yunquan.ohana.entity.LocalFrameEntity
import com.yunquan.ohana.entity.ScheduleModelVo
import com.yunquan.ohana.http.BaseHttp
import com.yunquan.ohana.http.exception.ApiException
import com.yunquan.ohana.http.observer.BaseObserver
import com.yunquan.ohana.http.transformer.CommonTransformer
import com.yunquan.ohana.utils.ToastUtils
import io.reactivex.rxjava3.disposables.Disposable
import okhttp3.RequestBody.Companion.toRequestBody

class SyncNewViewModel(application: Application) : BaseViewModel(application) {

    val showResult = SingleLiveEvent<Int>()

    var mDeviceId: String? = null
    var mRoleId: String? = null

    val dataList = mutableListOf<ScheduleModelVo>()

    fun uploadEvent(data: List<ScheduleModelVo>) {
        dataList.clear()
        dataList.addAll(data)
        val body = Gson().toJson(data).toRequestBody(BaseHttp.MEDIA_TYPE_JSON)
        BaseHttp.api.uploadEvents(body)
            .compose(CommonTransformer())
            .subscribe(object : BaseObserver<Int>() {
                override fun onSubscribe(d: Disposable) {
                    super.onSubscribe(d)
                    showLoading()
                }

                override fun onSuccess(result: Int?) {
                    super.onSuccess(result)
                    showResult.value = result ?: 0
                }

                override fun onComplete() {
                    super.onComplete()
                    hideLoading()
                }
            })
    }
}